//
//  GoogleReaderHelper.m
//  Feeder
//
//  Created by Diego Acosta on 8/1/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "GoogleReaderHelper.h"

#define kSortIdStringLength 8


@implementation GoogleReaderHelper

#pragma mark Class methods

+ (NSString *)unreadStringFromNumber:(NSNumber *)count forMaxCount:(NSUInteger)max {

	NSUInteger _count = [count intValue];
	NSMutableString * unread = [[[NSMutableString alloc] initWithFormat:@"%d", _count] autorelease];
	
	// append plus sign if value it's equal than max.
	if (_count == max) {
		
		[unread appendString:@"+"];
	}

	return (NSString *)unread;
}

+ (NSArray *)splitSortIdsFromString:(NSString *)sortid {

	NSMutableArray * _idlist = [[NSMutableArray alloc] init];
	for (int i = 0; i < [sortid length] / kSortIdStringLength; i++) {
		
		NSUInteger step = i * kSortIdStringLength;
		
		// substring sortid.
		NSRange range = NSMakeRange(step, kSortIdStringLength);
		NSString * chunk = [sortid substringWithRange:range];

		// add string into list.
		[_idlist addObject:chunk];
	}
	
	return _idlist;
}

+ (NSString *)tagIdforSortId:(NSString *)sortId onItemsList:(NSDictionary *)list {

	NSString * _id = @"";
	
	// search for sortid on list.
	for (NSString * _tagid in [list allKeys]) {
		
		NSDictionary * _item = [list objectForKey:_tagid];
		if ([[_item objectForKey:@"sortid"] isEqualToString:sortId]) {
			
			_id = [NSString stringWithString:_tagid];
			break;
		}
	}
	
	return _id;
}

+ (GRTagIdType)typeForTagId:(NSString *)tagId {

	NSArray * _id = [tagId componentsSeparatedByString:@"/"];
	
	if ([[_id objectAtIndex:0] isEqualToString:@"feed"]) {
		
		// tagid is a feed.
		return GRTagIdTypeFeed;
	}
	else if ([[_id objectAtIndex:0] isEqualToString:@"user"]) {
		
		if ([[_id objectAtIndex:2] isEqualToString:@"label"]) {
			
			// tagid is a label.
			return GRTagIdTypeLabel;
		}
		else if ([[_id objectAtIndex:2] isEqualToString:@"state"]) {
			
			if ([[_id objectAtIndex:3] isEqualToString:@"com.google"] &&
				[[_id objectAtIndex:4] isEqualToString:@"root"]) {
				
				// tagid is an state on google's root node.
				return GRTagIdTypeStateGoogleRoot;
			}
			else if ([[_id objectAtIndex:3] isEqualToString:@"com.google"] &&
					 [[_id objectAtIndex:4] isEqualToString:@"reading-list"]) {
				
				// tagid is an state on google's reading-list.
				return GRTagIdTypeStateGoogleReadingList;
			}
		}
	}
	
	return GRTagIdTypeUnknown;
}

@end
